package com.eagle.view.fragment.live;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.eagle.view.widget.LoadingDialog;

/**
 * Created by gc on 2017/1/10.
 */
public abstract class BaseFragment extends Fragment implements SensorEventListener {

    private LoadingDialog mDialog = null;

    protected SensorManager mSensorManager;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    }

    @Override
    public void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    public void showLoading() {
        try {
            mDialog = new LoadingDialog(getActivity());
            mDialog.show();
        } catch (Exception e) {
            mDialog = null;
        }
    }

    public void dismissLoading() {
        try {
            if (null != mDialog && mDialog.isShowing() && !getActivity().isFinishing()) {
                mDialog.cancel();
                mDialog = null;
            }
        } catch (Exception e) {

        }
    }

    public boolean isLoading() {
        if (mDialog != null) {
            return mDialog.isShowing();
        }
        return false;
    }
}
